package com.mybook.app.controller;

import com.mybook.app.core.BaseResponse;
import com.mybook.app.dto.TransAndBookInfoAndUserInfoDto;
import com.mybook.app.request.*;
import com.mybook.app.service.BorrowService;
import com.mybook.app.utils.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by zjutsw on 2017/11/10.
 */
@RestController
@RequestMapping("/borrow")
public class BorrowController {

    @Autowired
    private BorrowService borrowService;

    /**
     * 我的借阅
     *
     * @return
     */
    @PostMapping("list")
    public BaseResponse list(@RequestBody BorrowListRequest borrowListRequest) {
        Logger.debug(this, "查询我的借阅");
        return borrowService.list(borrowListRequest);
    }

    /**
     * 我的借阅
     *
     * @return
     */
    @PostMapping("list/keeper")
    public BaseResponse listKeeper(@RequestBody BorrowListRequest borrowListRequest) {
        Logger.debug(this, "查询我的借阅 (持有人)");
        return borrowService.listKeeper(borrowListRequest);
    }

    /**
     * 我的借阅
     *
     * @return
     */
    @PostMapping("list/keeper/detail")
    public BaseResponse listKeeperDetail(@RequestBody BorrowKeeperDetailRequest borrowListRequest) {
        Logger.debug(this, "查询我的借阅 (持有人) 详情");
        TransAndBookInfoAndUserInfoDto transAndBookInfoAndUserInfoDto =  borrowService.listKeeperDetail(borrowListRequest);
        return new BaseResponse(transAndBookInfoAndUserInfoDto);
    }


    /**
     * 申请借阅
     *
     * @return
     */
    @PostMapping("apply")
    public BaseResponse apply(@RequestBody BorrowApplyRequest borrowApplyRequest) {
        Logger.debug(this, "申请借阅");
        borrowService.apply(borrowApplyRequest);
        return BaseResponse.ok();
    }

    /**
     * 申请人
     * 申请后操作
     *
     * @param borrowCancelRequest
     * @return
     */
    @PostMapping("apply/oper")
    public BaseResponse applyOper(@RequestBody BorrowCancelRequest borrowCancelRequest) {
        Logger.info(this, "申请人 申请操作");
        borrowService.applyOper(borrowCancelRequest);
        return BaseResponse.ok();
    }

    /**
     * 持有人
     * 申请后操作
     *
     * @param borrowCancelRequest
     * @return
     */
    @PostMapping("oper")
    public BaseResponse oper(@RequestBody BorrowCancelRequest borrowCancelRequest) {
        Logger.info(this, "持有人 被申请操作");
        borrowService.oper(borrowCancelRequest);
        return BaseResponse.ok();
    }

}
